#include "qwtHistogram.h"
namespace LightChart {
qwtHistogram::qwtHistogram (BasicHistogram & histogram) : qwtPlotAdapter(histogram) {

}
void qwtHistogram::draw() {
}
void qwtHistogram::preparePlot(QwtPlot * plot) {
    BasicHistogram * hist = static_cast<BasicHistogram*>(&chart);
    std::map<Axis *, std::vector<Series*> > & axes = storage.charts[&chart];
    Axis * axis_x = (*axes.begin()).first;
    Axis * axis_y = (*++axes.begin()).first;
    std::vector<Series *> & interval_series = axes[axis_x];
    std::vector<Series *> & values_series = axes[axis_y];
    if (values_series.size() > 0) {
        if (interval_series.size() == values_series.size()) {
            for (unsigned int i = 0; i<interval_series.size(); ++i) {
                std::vector<double> & x = values_series[i]->get_double_vector();
                std::vector<double> & y = interval_series[i]->get_double_vector();
                QVector<QwtIntervalSample> samples(x.size());
                for(unsigned int j = 0; j < x.size(); ++j) {
                    QwtInterval interval(y[j], y[j+1]);
                    samples[j] = QwtIntervalSample(x[j], interval);
                }
                QwtPlotHistogram * histogram = new QwtPlotHistogram(values_series[i]->get_name().c_str());
                histogram->setData(new QwtIntervalSeriesData(samples));
                histogram->attach(plot);
            }
        }
        else if (interval_series.size() == 0 && hist->min != 0 && hist->max != 0) {
            double tmp_min = hist->min;
            for (unsigned int i = 0; i<interval_series.size(); ++i) {
                std::vector<double> & x = values_series[i]->get_double_vector();
                double inter = (hist->max-hist->min)/(double)x.size();
                QVector<QwtIntervalSample> samples(x.size());
                for(unsigned int j = 0; j < x.size(); ++j) {
                    QwtInterval interval(tmp_min, tmp_min+inter);
                    tmp_min += inter;
                    samples[j] = QwtIntervalSample(x[j], interval);
                }
                QwtPlotHistogram * histogram = new QwtPlotHistogram(values_series[i]->get_name().c_str());
                histogram->setData(new QwtIntervalSeriesData(samples));
                histogram->attach(plot);
            }
        }
    }
}
}
